package com.eliauk.demo;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;

public class CodeGenerator {

    public static void main(String[] args) {
        // 创建代码生成器
        AutoGenerator generator = new AutoGenerator();

        // 数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/test"); //数据库url
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");  //数据库驱动
        dataSourceConfig.setUsername("root");                        //数据库用户
        dataSourceConfig.setPassword("");                            //数据库密码
        dataSourceConfig.setDbType(DbType.MYSQL);
        generator.setDataSource(dataSourceConfig);

        // 全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOutputDir(System.getProperty("user.dir") + "/src/main/java");
        globalConfig.setOpen(false);
        globalConfig.setIdType(IdType.AUTO);
        globalConfig.setDateType(DateType.ONLY_DATE);
        generator.setGlobalConfig(globalConfig);
        //globalConfig.setFileOverride(true);//覆盖

        // 包配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent("com.eliauk");
        packageConfig.setModuleName("demo");
        packageConfig.setEntity("entity");
        packageConfig.setMapper("mapper");
        packageConfig.setController("controller");
        packageConfig.setService("service");
        packageConfig.setServiceImpl("service.impl");
        generator.setPackageInfo(packageConfig);

        // 策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setInclude("table_person"); // 要生成代码的表名
        generator.setStrategy(strategyConfig);

        // 执行生成
        generator.execute();
    }
}